import MyEnums from "./MyEnums";

class itrmCounter {
    day: number = 0;
    dayAwared: string[] = [];
    week: number = 0;
    weekAwared: string[] = [];
    month: number = 0;
    monthAwared: string[] = [];
    everAwared: string[] = [];
    timeday: number[] = [];//当前日期
    timeweek: number[] = [];//下周的日期
}

export default class skillCounter {

    counterDate: itrmCounter = null;

    getCounter(): itrmCounter {
        if (!this.counterDate)
            this.counterDate = gameHelper.getInstance().skillCounterParam;
        if (!this.counterDate) {
            this.counterDate = new itrmCounter();
            this.setnewtime();
            return this.counterDate;
        }
        return this.checkCounter();
    }
    setCounter(skill: number) {
        this.getCounter();
        this.counterDate.day += skill;
        this.seveCounter();
    }
    setAwared(id: string, timeType: number) {
        this.getCounter();
        switch (timeType) {
            case MyEnums.counterType.day:
                this.counterDate.dayAwared.push(id);
                break;
            case MyEnums.counterType.month:
                this.counterDate.monthAwared.push(id);
                break;
            case MyEnums.counterType.week:
                this.counterDate.weekAwared.push(id);
                break;
            default:
                this.counterDate.everAwared.push(id);
                break;
        }
        this.seveCounter();
    }
    checkAwared(id: string, timeType: number) {
        this.getCounter();
        let date = [];
        switch (timeType) {
            case MyEnums.counterType.day:
                date = this.counterDate.dayAwared;
                break;
            case MyEnums.counterType.month:
                date = this.counterDate.monthAwared;
                break;
            case MyEnums.counterType.week:
                date = this.counterDate.weekAwared;
                break;
            default:
                date = this.counterDate.everAwared;
                break;
        }
        if (date.indexOf(id) > -1)
            return true;
        else
            return false;
    }
    private seveCounter() {
        gameHelper.getInstance().skillCounterParam = this.counterDate;
    }
    private checkCounter(): itrmCounter {
        let time = new Date();
        let date = time.getDate();
        let month = time.getMonth();
        if (this.counterDate.timeday[0] != date || this.counterDate.timeday[1] != month) {
            this.counterDate.month += this.counterDate.day;
            this.counterDate.week += this.counterDate.day;
            if (this.counterDate.timeday[1] < month) {
                this.counterDate.month = 0;
                this.counterDate.monthAwared = [];
            }
            else if (this.counterDate.timeday[1] == 12 && month == 1) {
                this.counterDate.month = 0;
                this.counterDate.monthAwared = [];
            }

            if (this.counterDate.timeweek[0] <= date || this.counterDate.timeweek[1] < month) {
                this.counterDate.week = 0;
                this.counterDate.weekAwared = [];
                let weekTime = this.getNewWeek();
                this.counterDate.timeweek = [weekTime.getDate(), weekTime.getMonth()];
            }

            this.counterDate.day = 0;
            this.counterDate.dayAwared = [];
            this.counterDate.timeday = [date, month];
            this.seveCounter();
        }
        return this.counterDate;
    }
    private setnewtime() {
        let time = new Date();
        this.counterDate.timeday = [time.getDate(), time.getMonth()];
        let weekTime = this.getNewWeek();
        this.counterDate.timeweek = [weekTime.getDate(), weekTime.getMonth()];
    }
    private getNewWeek(): Date {
        let t = new Date();
        let week = t.getDay();
        let addTime = 7 - week;
        if (addTime == 0)
            addTime = 7;
        return new Date(t.getFullYear(), t.getMonth(), t.getDate() + addTime);
    }
}
